<?php
/**
 * SellerController.php
 *
 * @copyright  2023 beikeshop.com - All Rights Reserved
 * @link       https://beikeshop.com
 * @author     TL <mengwb@guangda.work>
 * @created    2023-04-24 18:57:56
 * @modified   2023-04-24 18:57:56
 */

namespace Plugin\MultiSeller\Http\Controllers\Admin;

use Beike\Admin\Http\Controllers\Controller;
use Beike\Seller\Models\SellerUser;
use Beike\Seller\Repositories\SellerGroupRepo;
use Beike\Seller\Repositories\SellerRepo;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;
use Plugin\MultiSeller\Http\Requests\SellerRequest;
use Plugin\MultiSeller\Http\Resources\SellerGroupResource;
use Plugin\MultiSeller\Http\Resources\SellerResource;
use Plugin\MultiSeller\Services\SellerService;

class SellerController extends Controller
{
    /**
     * @param Request $request
     * @return View
     */
    public function index(Request $request): View
    {
        $sellers = SellerRepo::list($request->only(['email', 'status', 'like_store_name', 'seller_group_id']));
        $data    = [
            'locales'        => locales(),
            'sellers'        => $sellers,
            'seller_groups'  => SellerGroupResource::collection(SellerGroupRepo::list())->jsonSerialize(),
        ];

        return view('MultiSeller::admin.sellers.index', $data);
    }

    /**
     * @param SellerRequest $request
     * @return array
     */
    public function store(SellerRequest $request)
    {
        $requestData = $request->all();

        $seller = SellerService::create($requestData);

        return json_success(trans('common.success'), new SellerResource($seller));
    }

    public function create()
    {
        $seller    = new \Beike\Seller\Models\Seller();
        $data      = [
            'seller'        => $seller,
            'seller_groups' => SellerGroupResource::collection(SellerGroupRepo::list())->jsonSerialize(),
        ];

        return view('MultiSeller::admin.sellers.form', $data);
    }

    /**
     * @param int $id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    public function edit(int $id)
    {
        $seller    = SellerRepo::find($id);
        $data      = [
            'seller'        => $seller,
            'seller_groups' => SellerGroupResource::collection(SellerGroupRepo::list())->jsonSerialize(),
        ];

        return view('MultiSeller::admin.sellers.form', $data);
    }

    /**
     * @param SellerRequest $request
     * @param int           $id
     * @return array
     */
    public function update(SellerRequest $request, int $id)
    {
        $requestData = $request->all();

        $seller = SellerService::update($id, $requestData);
        return json_success(trans('common.updated_success'), $seller);
    }

    /**
     * @param Request $request
     * @param int     $id
     * @return void
     * @throws \Exception
     */
    public function destroy(Request $request, int $id)
    {
        return; // 商户不能删除；
        SellerRepo::delete($id);

        return json_success(trans('common.deleted_success'));
    }

    /**
     * @param Request $request
     * @param int     $id
     * @return array
     */
    public function approve(Request $request, int $id)
    {
        $status = $request->get('status');
        SellerRepo::approve($id, $status);

        return json_success(trans('common.updated_success'));
    }

    /**
     * @param int $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function loginSeller(int $id)
    {
        $seller = SellerRepo::find($id);
        Auth::guard(SellerUser::AUTH_GUARD)->loginUsingId($seller->users()->where('is_root', 1)->value('id'));

        return redirect()->to(seller_route('home.index'));
    }

    /**
     * @param Request $request
     * @return JsonResponse
     */
    public function autocomplete(Request $request): JsonResponse
    {
        try {
            $items = SellerRepo::autocomplete($request->get('name') ?? '', 0);
        } catch (Exception $e) {
            return json_fail($e->getMessage(), []);
        }

        return json_success(trans('common.get_success'), $items);
    }

}
